using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Presentation;
using Presentation.Contracts;
using System.Collections.Generic;

namespace ProjektZespolowy.Forum
	{
	public partial class ShowForum : System.Web.UI.Page, IForumView
		{
		private ForumPresenter presenter;
		private int commentStartIndex = 0;

		protected override void OnInit(EventArgs e)
			{
			base.OnInit(e);
			if (!string.IsNullOrEmpty(Request.QueryString["ForumID"]))
				ViewState["ForumID"] = Request.QueryString["ForumID"];
			if (!string.IsNullOrEmpty(Request.QueryString["ParentCommentID"]))
				ViewState["ParentCommentID"] = Request.QueryString["ParentCommentID"];

			presenter = new ForumPresenter(this);
			presenter.PageComments();
			}



		protected void Page_Load(object sender, EventArgs e)
			{
			if (!IsPostBack)
				{
				
				presenter.Initialize();
				}
				
			}

		protected void deleteForumButton_Click(object sender, EventArgs e)
			{

			}

		protected void unsignForumButton_Click(object sender, EventArgs e)
			{

			}

		#region IForumView Members

		public void PageComments(int commentsPerPage, int howManyComments)
			{
			int counter = 1;
			List<LinkButton> returnList = new List<LinkButton>();
			for (int i = 0; i < howManyComments; i += 10)
				{
				LinkButton tmp = new LinkButton();
				int st = 0 + (counter - 1) * 10;
				tmp.Text = counter.ToString();
				returnList.Add(tmp);
				counter++;
				}
			counter = 0;
			foreach (LinkButton hl in returnList)
				{
				this.Panel3.Controls.Add(hl);
				hl.ID = "PageMyForumsButton" + counter.ToString();
				hl.Click += new EventHandler(PagingButton_Click);
				Label tmpLabel = new Label();
				tmpLabel.Text = " | ";
				this.Panel3.Controls.Add(tmpLabel);
				counter++;
				}
			}


		public void ShowComments(DataTable comments)
			{
			Repeater1.DataSource = comments;
			Repeater1.DataBind();
			}

		public int ForumID
			{
			get 
				{
				if (ViewState["ForumID"] != null)
					return Int32.Parse((string)ViewState["ForumID"]);
				else
					return -1;
				}
			}


		public int ParentCommentID
			{
			get
				{
				if (ViewState["ParentCommentID"] != null)
					return Int32.Parse((string)ViewState["ParentCommentID"]);
				else
					return -1;
				}
			}

		public int CommentsStartIndex
			{
			get { return this.commentStartIndex; }
			}

		
		public bool deleteButtonVisible
			{
			set { this.deleteForumButton.Visible = value; }
			}

		public bool unsignButtonVisible
			{
			set {}
			}

		public bool newTopicButtonVisible
			{
			set { this.newTopicButton.Visible = value; }
			}

		#endregion

		#region IUserAuthView Members

		public int LoggedUserID
			{
			get
				{
				if (Session[Globals.ContentNamesGlobals.SessionWithUserId] != null)
					return Int32.Parse(Session[Globals.ContentNamesGlobals.SessionWithUserId].ToString());
				else
					return -1;
				}
			}

		#endregion

		protected void newTopicButton_Click(object sender, EventArgs e)
			{
			Response.Redirect("~/Forum/AddComment.aspx?ForumID=" + this.ForumID.ToString() + "&ParentCommentID=" + this.ParentCommentID.ToString());
			}

		public string ReplyLinkString(string binderString)
			{
			if (string.IsNullOrEmpty(binderString))
				return "";
			else
				return "Odpowiedz";
			}

		public string ImageUrl(string user_id)
			{
			if (presenter.hasAvatar(Int32.Parse(user_id)))
				{
				return "../GetUserPicture.aspx?id=" + user_id;
				}
			else
				return "../images/blank-photo.gif";
			}

		protected void replyButton_Click(object sender, EventArgs e)
			{
			Response.Redirect("~/Default.aspx");
			}

		protected void PagingButton_Click(object sender, EventArgs e)
			{
			this.commentStartIndex = (Int32.Parse(((LinkButton)(sender)).Text) - 1) * 10;
			presenter.Initialize();
			}

		protected void backToForumButton_Click(object sender, EventArgs e)
			{
			Response.Redirect("~/Forum/ShowTopics.aspx?ForumID=" + this.ForumID);
			}


		#region IForumView Members



		#endregion

		#region IForumView Members


		

		#endregion
		}
	}
